package com.sz.jinzheng.controller;

import com.sz.jinzheng.model.BillForm;
import com.sz.jinzheng.model.Interest;
import com.sz.jinzheng.model.Result;
import com.sz.jinzheng.service.TbillFormService;
import com.sz.jinzheng.until.TimeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Slf4j
public class GenerateBill {
    @Autowired
    private TbillFormService tbillFormService;

    public Result getInterest(Integer id) {//借款时调用账单录入账单表
        log.info("=======接收id="+id);

        Interest s1 = tbillFormService.getInterest(id);//通过用户id调用interest方法查询
        Double amount = s1.getAmount();//这是借款本金
        Double borrowYearRate = s1.getBorrowYearRate();//这是年利率
        Integer period = s1.getPeriod();//这是借款期限
        log.info("222222");
        log.info(s1.toString());
        Integer s = s1.getReturnMethod();//获得还款方式
        log.info("23245323");
        Integer userid = s1.getUserId();//id
        log.info("===================");
        Integer billFormId = id;//这是账单id
        Integer id1 = s1.getId();//这是借款信息编号
        Integer borrowerInfoId = s1.getBorrowerInfoId();//借款人信息表ID
        long l = System.currentTimeMillis();//获取系统时间
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");//获取当前时间
        Timestamp createTime = s1.getCreateTime();//获得用户借款时间
        long time = createTime.getTime();//创建时间转换long；
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        if (s == 1) {//等额本息
            for (int i = 0; i < period; i++) {
                double pow = Math.pow((1 + borrowYearRate / 12), period);
                log.info("=====" + pow + "------------------");
                double principalAndInterest = amount * borrowYearRate / 12 * pow / (pow - 1);//本加息
                Double capital = amount / period;//每月本金
                Double interest = principalAndInterest - capital;//每月利息
                long t = ((long) 1000 * (long) 24 * (long) 3600 * (long) 30) * i;
                long q = createTime.getTime() + t;//开始还款时间
                String startTime = TimeUtils.longToDate(q);//将long转换成时间戳
                long w = q + ((long) 1000 * (long) 24 * (long) 3600 * (long) 30);//开始的时间加上第几个月时间
                String endtime = TimeUtils.longToDate(w);//将long转换成时间戳
                long e = System.currentTimeMillis();//获取修改时间
                String updateTime = TimeUtils.longToDate(e);//将long转换成时间戳
//                int whichIssue = Math.toIntExact((q - time) / ((long) 1000 * (long) 24 * (long) 3600 * (long) 30)) + 1;//第几期
                int whichIssue = 0;
                int insertbill = 0;
                for (int p = 0; p < period; p++) {
                    try {
                        Date datestartTime = sf.parse(startTime);
                        Date dateendtime = sf.parse(endtime);
                        //使用SimpleDateFormat的parse()方法生成Date
                        Date dateupdateTime = sf.parse(updateTime);
                        //打印Date
                        whichIssue++;//期数自增
                        log.info("这是期数=" + whichIssue + "这是期数=" + period);
                        BillForm billForm = new BillForm();
                        billForm.setUserId(userid);
                        billForm.setBillFormId(billFormId);
                        billForm.setBorrowInfoId(borrowerInfoId);
                        billForm.setWhichIssue(whichIssue);
                        billForm.setCapital(capital);
                        billForm.setPrincipalAndInterest(principalAndInterest);
                        billForm.setInterest(interest);
                        billForm.setStatus(0);
                        billForm.setOverdueDays(0);
                        billForm.setPenaltyRate(0.05);
                        billForm.setPenaltyInterest(0.00);
                        billForm.setStartTime(datestartTime);
                        billForm.setEndTime(dateendtime);
                        billForm.setCreateTime(createTime);
                        billForm.setUpdateTime(dateupdateTime);
                        billForm.setIsDeleted(0);
                        insertbill = tbillFormService.insertbill(billForm);

                    } catch (ParseException e1) {
                        e1.printStackTrace();
                    }
                }
                return new Result(200, "插入成功", insertbill);

            }

        }
        if (s == 3) {//这是先息后本
            Double capital = null;//本金
            Double principalAndInterest = null;//每月本息
            Double interest = amount * borrowYearRate / 12;//1-12月利息
            if (period == 12) {//11+1
                for (int i = 0; i < 12; i++) {
                    if (i < 11) {
                        capital = 0.00;//2-11月还的本金
                        principalAndInterest = capital + interest;//每月本息
                    }
                    if (i > 10 && i < 12) {//第十二月
                        capital = amount;//第十二个月应还的全款
                        principalAndInterest = capital + interest;//12月本息
                    }
                    long t = ((long) 1000 * (long) 24 * (long) 3600 * (long) 30) * i;
                    long q = createTime.getTime() + t;//开始还款时间
                    String startTime = TimeUtils.longToDate(q);//将long转换成时间戳
                    long w = q + ((long) 1000 * (long) 24 * (long) 3600 * (long) 30);//开始的时间加上第几个月时间
                    String endtime = TimeUtils.longToDate(w);//将long转换成时间戳
                    long e = System.currentTimeMillis();//获取修改时间
                    String updateTime = TimeUtils.longToDate(e);//将long转换成时间戳
                    int whichIssue = Math.toIntExact((q - time) / ((long) 1000 * (long) 24 * (long) 3600 * (long) 30)) + 1;//第几期
                    try {
                        Date datestartTime = sf.parse(startTime);
                        Date dateendtime = sf.parse(endtime);
                        //使用SimpleDateFormat的parse()方法生成Date
                        Date dateupdateTime = sf.parse(updateTime);
                        //打印Date
                        BillForm billForm = new BillForm();
                        billForm.setUserId(userid);
                        billForm.setBillFormId(billFormId);
                        billForm.setBorrowInfoId(borrowerInfoId);
                        billForm.setWhichIssue(whichIssue);
                        billForm.setCapital(capital);
                        billForm.setPrincipalAndInterest(principalAndInterest);
                        billForm.setInterest(interest);
                        billForm.setStatus(0);
                        billForm.setOverdueDays(0);
                        billForm.setPenaltyRate(0.05);
                        billForm.setPenaltyInterest(0.00);
                        billForm.setStartTime(datestartTime);
                        billForm.setEndTime(dateendtime);
                        billForm.setCreateTime(createTime);
                        billForm.setUpdateTime(dateupdateTime);
                        billForm.setIsDeleted(0);
                        tbillFormService.insertbill(billForm);
                    } catch (ParseException e1) {
                        e1.printStackTrace();
                    }
                }
            }
            if (period == 18) {//6+12
                for (int i = 0; i < 18; i++) {
                    if (i < 6) {
                        capital = 0.00;//2-11月还的本金
                        principalAndInterest = capital + interest;//每月本息
                    }
                    if (i > 5 && i < 18) {//第十二月
                        capital = amount / 12;//第7-18个月应还的全款
                        principalAndInterest = capital + interest;//12月本息
                    }
                    long t = ((long) 1000 * (long) 24 * (long) 3600 * (long) 30) * i;
                    long q = createTime.getTime() + t;//开始还款时间
                    String startTime = TimeUtils.longToDate(q);//将long转换成时间戳
                    long w = q + ((long) 1000 * (long) 24 * (long) 3600 * (long) 30);//开始的时间加上第几个月时间
                    String endtime = TimeUtils.longToDate(w);//将long转换成时间戳
                    long e = System.currentTimeMillis();//获取修改时间
                    String updateTime = TimeUtils.longToDate(e);//将long转换成时间戳
                    int whichIssue = Math.toIntExact((q - time) / ((long) 1000 * (long) 24 * (long) 3600 * (long) 30)) + 1;//第几期
                    try {
                        Date datestartTime = sf.parse(startTime);
                        Date dateendtime = sf.parse(endtime);
                        //使用SimpleDateFormat的parse()方法生成Date
                        Date dateupdateTime = sf.parse(updateTime);
                        //打印Date
                        BillForm billForm = new BillForm();
                        billForm.setUserId(userid);
                        billForm.setBillFormId(billFormId);
                        billForm.setBorrowInfoId(borrowerInfoId);
                        billForm.setWhichIssue(whichIssue);
                        billForm.setCapital(capital);
                        billForm.setPrincipalAndInterest(principalAndInterest);
                        billForm.setInterest(interest);
                        billForm.setStatus(0);
                        billForm.setOverdueDays(0);
                        billForm.setPenaltyRate(0.05);
                        billForm.setPenaltyInterest(0.00);
                        billForm.setStartTime(datestartTime);
                        billForm.setEndTime(dateendtime);
                        billForm.setCreateTime(createTime);
                        billForm.setUpdateTime(dateupdateTime);
                        billForm.setIsDeleted(0);
                        tbillFormService.insertbill(billForm);
                    } catch (ParseException e1) {
                        e1.printStackTrace();
                    }
                }
            }
            if (period == 21) {//9+12
                for (int i = 0; i < 21; i++) {
                    if (i < 9) {
                        capital = 0.00;//2-11月还的本金
                        principalAndInterest = capital + interest;//每月本息
                    }
                    if (i > 8 && i < 21) {//第十二月
                        capital = amount / 12;//第7-18个月应还的全款
                        principalAndInterest = capital + interest;//12月本息
                    }
                    long t = ((long) 1000 * (long) 24 * (long) 3600 * (long) 30) * i;
                    long q = createTime.getTime() + t;//开始还款时间
                    String startTime = TimeUtils.longToDate(q);//将long转换成时间戳
                    long w = q + ((long) 1000 * (long) 24 * (long) 3600 * (long) 30);//开始的时间加上第几个月时间
                    String endtime = TimeUtils.longToDate(w);//将long转换成时间戳
                    long e = System.currentTimeMillis();//获取修改时间
                    String updateTime = TimeUtils.longToDate(e);//将long转换成时间戳
                    int whichIssue = Math.toIntExact((q - time) / ((long) 1000 * (long) 24 * (long) 3600 * (long) 30)) + 1;//第几期
                    try {
                        Date datestartTime = sf.parse(startTime);
                        Date dateendtime = sf.parse(endtime);
                        //使用SimpleDateFormat的parse()方法生成Date
                        Date dateupdateTime = sf.parse(updateTime);
                        //打印Date
                        BillForm billForm = new BillForm();
                        billForm.setUserId(userid);
                        billForm.setBillFormId(billFormId);
                        billForm.setBorrowInfoId(borrowerInfoId);
                        billForm.setWhichIssue(whichIssue);
                        billForm.setCapital(capital);
                        billForm.setPrincipalAndInterest(principalAndInterest);
                        billForm.setInterest(interest);
                        billForm.setStatus(0);
                        billForm.setOverdueDays(0);
                        billForm.setPenaltyRate(0.05);
                        billForm.setPenaltyInterest(0.00);
                        billForm.setStartTime(datestartTime);
                        billForm.setEndTime(dateendtime);
                        billForm.setCreateTime(createTime);
                        billForm.setUpdateTime(dateupdateTime);
                        billForm.setIsDeleted(0);
                        tbillFormService.insertbill(billForm);
                    } catch (ParseException e1) {
                        e1.printStackTrace();
                    }
                }
            }
            return new Result(200, "成功", null);

        }
        return new Result(101, "已还状态不对", null);
    }
}
